﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Threading;
namespace OtherDevice
{
    public partial class Form_SixAxisRobot : Form_ControlBase
    {
        public Form_SixAxisRobot()
        {
            InitializeComponent();
        }
        SixAxisRobot_Base robot = null;
        public override void FlushToDlg(object devicebase)
        {
            try
            {
                if (devicebase == null)
                    return;
                this.robot = devicebase as SixAxisRobot_Base;
                this.Dock = DockStyle.Fill;
                this.Show();
            }
            catch (Exception)
            {

            }
        }
        private void Form_SixAxisRobot_Load(object sender, EventArgs e)
        {
            chb_InputIo.Items.Clear();
            chb_outputIo.Items.Clear();

            for (int i = 0; i < 32; i++)
            {
                chb_InputIo.Items.Add(i);
                chb_outputIo.Items.Add(i);
            }
            chb_InputIo.SelectedIndex = 0;
            chb_outputIo.SelectedIndex = 0;
            this.Txt_X.Enabled = false;
            this.Txt_Y.Enabled = false;
            this.Txt_Z.Enabled = false;
            this.Txt_W.Enabled = false;
            this.Txt_P.Enabled = false;
            this.Txt_R.Enabled = false;

            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    PointWorld point = robot.GetCurrentPos_World();
                    BeginInvoke(new Action(() =>
                    {
                        Txt_X.Text = point.X.ToString("F2");
                        Txt_Y.Text = point.Y.ToString("F2");
                        Txt_Z.Text = point.Z.ToString("F2");
                        Txt_W.Text = point.W.ToString("F2");
                        Txt_P.Text = point.P.ToString("F2");
                        Txt_R.Text = point.R.ToString("F2");
                        if (robot.IsConnect())
                        {
                            lbl_ConnectState.BackColor = Color.Green;
                            lbl_ConnectState.Text = "已连接";
                        }
                        else
                        {
                            lbl_ConnectState.BackColor = Color.Red;
                            lbl_ConnectState.Text = "未连接";
                        }
                    })); 
                }
            });
        }

        private void btn_Sevro_Click(object sender, EventArgs e)
        {
            try
            {
                if (btn_Sevro.Text == "使能")
                {
                    robot.SetServo(true);
                    btn_Sevro.Text = "失能";
                }
                else
                {
                    robot.SetServo(false);
                    btn_Sevro.Text = "使能";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void btn_ReadIo_Click(object sender, EventArgs e)
        {
            try
            {
                int nIndex = Convert.ToInt32(chb_InputIo.Text);
                bool bState = robot.ReadDigitalInPort(nIndex);
                if (bState)
                {
                    lightEmg0.OffColor = Color.Green;
                }
                else
                {
                    lightEmg0.OffColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btn_On_Click(object sender, EventArgs e)
        {
            try
            {
                int nIndex = Convert.ToInt32(chb_outputIo.Text);
                robot.SetDigitalOutputPort(nIndex, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btn_Off_Click(object sender, EventArgs e)
        {
            try
            {
                int nIndex = Convert.ToInt32(chb_outputIo.Text);
                robot.SetDigitalOutputPort(nIndex, false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
    }
}
